﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using System.Drawing.Drawing2D;
using FaceRecognition.DataBase;
using FaceRecognition.Models;

namespace FaceRecognition
{
    public partial class Form_Begin : Form
    {

        Form_AddUser add_form;
        Controller_Class control;
        public Form_Begin()
        {
            InitializeComponent();
            control = new Controller_Class();



        }




        private void Form1_Load(object sender, EventArgs e)
        {
            // face_detect_object = new Face_Detection();
            //// 45 mins

            //// passing 0 gets zeroth webcam
            //cap = new Capture(0);//open camera func
            //cap.Dispose();
            //// adjust path to find your xml
            //haar = new HaarCascade("..\\haarcascade_frontalface_alt2.xml");
        }

        private void Add_User_Click(object sender, EventArgs e)
        {

            //this.Hide();
            //if (add_form != null)
            //{
            //    add_form.Show();
            //    return;

            //}
            //    Add_User.Enabled = false;
            add_form = new Form_AddUser();
            add_form.Show();

            //control.add_user_form();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            control.open_image();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //int i = 0;

            //using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
            //{
            //    if (nextFrame != null)
            //    {

            //    //    // there's only one channel (greyscale), hence the zero index
            //    //    //var faces = nextFrame.DetectHaarCascade(haar)[0];
            //    //    Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
            //    //    var faces = grayframe.DetectHaarCascade(
            //    //                    haar, 1.4, 4,
            //    //                    HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
            //    //                    new Size(nextFrame.Width / 8, nextFrame.Height / 8)
            //    //                    )[0];

            //    //    foreach (var face in faces)
            //    //    {
            //    //        i++;
            //    //        nextFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 3);


            //    //    }

            //        pictureBox1.Image = nextFrame.ToBitmap();

            //      //  pictureBox1.Image = resizeImage(nextFrame.ToBitmap(),new Size(128, 128));
            //    }
            //}
        }
        private static Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;

        }

    }
}
